import {Component, Input, OnInit} from '@angular/core';
import {DocService} from '../../service/doc.service';
import {Entity} from '../../entity/entity';
import {RequestParamItem} from '../../entity/request-param-item';
import {ArrayUtils} from '../../utils/array-utils';
import {InvokeDataDubbo} from '../../entity/invoke-data-dubbo';
import {InvokeUtils} from '../../utils/invoke-utils';
import {MessageService} from '../../service/message.service';
import {AlertMessage} from '../../entity/alert-message';

@Component({
    selector: 'app-invoke-request-param-list',
    templateUrl: './invoke-request-param-list.component.html',
    styleUrls: ['./invoke-request-param-list.component.css']
})
export class InvokeRequestParamListComponent implements OnInit {
    private _param: RequestParamItem | InvokeDataDubbo;

    private entity: Entity;

    /**
     * 参数层级
     * @type {number}
     */
    @Input()
    level = 0;

    /**
     * 是否可添加子元素，用于列表、Map
     */
    @Input()
    parent: any;

    /**
     * Map.key
     */
    @Input()
    key: string;

    /**
     * 在集合里的位置
     */
    @Input()
    listIndex: number;

    @Input()
    listCount: number;

    /**
     * 添加Map时，使用的key
     */
    mapKey: string;

    /**
     * 类型：0=基本类型 1=枚举 2=pojo 3=collection 4=map
     */
    type: number;

    constructor(private messageService: MessageService,
                private docService: DocService) {
    }

    ngOnInit() {
    }

    get param(): RequestParamItem | InvokeDataDubbo {
        return this._param;
    }

    @Input()
    set param(value: RequestParamItem | InvokeDataDubbo) {
        this._param = value;

        if (!value['apiKey']) {
            const paramItem: RequestParamItem = <RequestParamItem>this._param;
            this.entity = this.docService.getEntity(paramItem.typeName);
        }
    }

    expand(param: RequestParamItem) {
        param.expanding = !param.expanding;
    }

    addItem() {
        // 获取当前的实体
        if (!this.entity.array && !this.entity.parameteredEntityRefs) {
            // 没有泛型...
            return;
        }
        let entityName: string;
        if (this.entity.array ) {
            // 数组
            console.log(this.entity);
            const index = this.entity.name.indexOf('[');
            entityName = this.entity.name.substring(0, index);
        } else {
            entityName = this.entity.parameteredEntityRefs[0].entityName;
        }

        const item = InvokeUtils.getListParameteredRequestItem(entityName, this.docService);
        if (!this.param['value']) {
            this.param['value'] = [];
        }
        this.param['value'].push(item);
    }

    delItem(item: RequestParamItem) {
        const index = ArrayUtils.indexOf(this.parent.value, item);
        if (index > -1) {
            this.parent.value.splice(index, 1);
        }
        if (this.parent.value && this.parent.value.length === 0) {
            this.parent.value = null;
        }
    }

    addMapItem() {
        if (!this.mapKey) {
            this.messageService.add(<AlertMessage>{
                id: 'map-key-error',
                type: 'error',
                html: '添加键值不能为空！',
                time: 5
            });
            return;
        }
        const entityRef = this.entity.parameteredEntityRefs[1];
        const item: RequestParamItem = InvokeUtils.getListParameteredRequestItem(entityRef.entityName, this.docService);
        item.mapKey = this.mapKey;
        if (!this.param['value']) {
            this.param['value'] = [];
        }
        this.param['value'].push(item);
        this.mapKey = null;
    }

    moveUp(param: RequestParamItem | InvokeDataDubbo) {
        const list = this.parent['value'];
        const currentItem = list[this.listIndex]; // 当前元素
        if (!currentItem) {
            return;
        }
        if (this.listIndex <= 0) {
            // 超出边界
            return;
        }
        list[this.listIndex] = list[this.listIndex - 1];
        list[this.listIndex - 1] = currentItem;
    }

    moveDown(param: RequestParamItem | InvokeDataDubbo) {
        const list = this.parent['value'];
        const currentItem = list[this.listIndex]; // 当前元素
        if (!currentItem) {
            return;
        }
        if (list >= this.listIndex + 1) {
            // 超出边界
            return;
        }
        list[this.listIndex] = list[this.listIndex + 1];
        list[this.listIndex + 1] = currentItem;
    }
}
